package com.baichang.taoshunda.utils;

import com.alibaba.nls.client.AccessToken;
import com.aliyuncs.exceptions.ClientException;
import com.baichang.taoshunda.entity.em.PushType;
import net.sf.json.JSONObject;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 17/09/19.
 */
public class UserPushMsgUtil {
    /**
     * 用户付款成功（给商家推）
     *
     * @param id          订单编号
     * @param userId      用户编号
     * @param phone       商家账号（手机号）
     * @param orderNumber
     * @param orderType
     */
    public static void sumbitOrderPush(Integer id, Integer userId, String phone, String orderNumber, Integer orderType) {
        String title = "您有新的淘瞬达订单，请注意查看！";
        String content = "您店铺的商品有用户下单啦，请及时点击查看";
        Map<String, Object> newMap = new HashMap<String, Object>();
        newMap.put("userId", userId);
        newMap.put("orderId", id);
        newMap.put("orderType", orderType);
        newMap.put("orderNumber", orderNumber);
        newMap.put("pushType", PushType.订单.getCode());
        newMap.put("sound", "news.mp3");
        JSONObject jsonObject = JSONObject.fromObject(newMap);
        UserJPushHelper jPushHelper = new UserJPushHelper("business.properties");
        jPushHelper.sendByAlias(title, content, jsonObject.toString(), phone, "您有新的淘瞬达订单，请打开淘瞬达商家端查看接单。.mp3");
    }

    /**
     * 用户付款成功（给商家推）
     *
     * @param
     * @param phone       商家账号（手机号）
     * @param money
     * @param id
     * @param orderNumber
     */
    public static void selfGoodsOrderPush(String phone, BigDecimal money, Integer id, String orderNumber) {
        String title = "淘瞬达自提订单已完成，用户已确认取货！";
        String content = "淘瞬达自提订单已完成，到账" + money + "元";
        Map<String, Object> newMap = new HashMap<String, Object>();
        AccessToken accessToken = null;
        try {
            accessToken = AccessToken.apply("LTAIJKQzy74ABj4D", "H3rmpA6auPSmGvcmLHuQhjv9EcDf5R");
            String appKey = "wLjCEly4Mc4eK2IY";
            String token = accessToken.getToken();
            newMap.put("pushType", PushType.自提订单.getCode());
            newMap.put("token", token);
            newMap.put("appKey", appKey);
            newMap.put("orderId", id);
            newMap.put("orderNumber", orderNumber);
            newMap.put("content", content);
            JSONObject jsonObject = JSONObject.fromObject(newMap);
            UserJPushHelper jPushHelper = new UserJPushHelper("business.properties");
            jPushHelper.sendByAlias(title, content, jsonObject.toString(), phone, "");
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 订单完成推送
     *
     * @param
     * @param phone       商家账号（手机号）
     * @param money
     * @param id
     * @param orderNumber
     */
    public static void completedOrder(String phone, BigDecimal money, Integer id, String orderNumber) {
        String title = "淘瞬达订单已完成！";
        String content = "淘瞬达订单已完成，到账" + money + "元";
        Map<String, Object> newMap = new HashMap<String, Object>();
        AccessToken accessToken = null;
        try {
            accessToken = AccessToken.apply("LTAIJKQzy74ABj4D", "H3rmpA6auPSmGvcmLHuQhjv9EcDf5R");
            String appKey = "wLjCEly4Mc4eK2IY";
            String token = accessToken.getToken();
            newMap.put("pushType", PushType.订单.getCode());
            newMap.put("token", token);
            newMap.put("appKey", appKey);
            newMap.put("orderId", id);
            newMap.put("orderNumber", orderNumber);
            newMap.put("content", content);
            JSONObject jsonObject = JSONObject.fromObject(newMap);
            UserJPushHelper jPushHelper = new UserJPushHelper("business.properties");
            jPushHelper.sendByAlias(title, content, jsonObject.toString(), phone, "");
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户确认消费
     *
     * @param id
     * @param userId
     * @param phone
     * @param orderNumber
     * @param orderType
     */
    public static void userConsumeOrder(Integer id, Integer userId, String phone, String orderNumber, Integer orderType) {
        String title = "您有淘瞬达预约单用户已确认消费，请注意查看！";
        String content = "您有淘瞬达预约单用户已确认消费，请注意查看";
        Map<String, Object> newMap = new HashMap<String, Object>();
        newMap.put("userId", userId);
        newMap.put("orderId", id);
        newMap.put("orderType", orderType);
        newMap.put("orderNumber", orderNumber);
        newMap.put("pushType", PushType.订单.getCode());
        newMap.put("sound", "");
        JSONObject jsonObject = JSONObject.fromObject(newMap);
        UserJPushHelper jPushHelper = new UserJPushHelper("business.properties");
        jPushHelper.sendByAlias(title, content, jsonObject.toString(), phone, "default");
    }

    /**
     * 用户催单订单处于待配送催商家
     *
     * @param id          订单编号
     * @param userId      用户编号
     * @param phone       商家账号（手机号）
     * @param orderNumber 订单单号
     * @param orderType
     */
    public static void reminderOrderPushByOrderStateToBusiness(Integer id, Integer userId, String phone, String orderNumber, Integer orderType) {
//        String title = "订单：" + orderNumber + " 的用户催单";
        String title = "您的订单被用户催单了，请及时处理";
//        String content = "您的订单:" + orderNumber + ",被用户催单了，请及时处理";
        String content = "您的订单被用户催单了，请及时处理";
        Map<String, Object> newMap = new HashMap<String, Object>();
        newMap.put("userId", userId);
        newMap.put("orderId", id);
        newMap.put("orderType", orderType);
        newMap.put("orderNumber", orderNumber);
        newMap.put("pushType", PushType.订单.getCode());
        newMap.put("sound", "cuidan.mp3");
        JSONObject jsonObject = JSONObject.fromObject(newMap);
        UserJPushHelper jPushHelper = new UserJPushHelper("business.properties");
        jPushHelper.sendByAlias(title, content, jsonObject.toString(), phone, "cuidan.mp3");
    }

    /**
     * 用户申请退款
     *
     * @param id          订单编号
     * @param userId      用户编号
     * @param phone       商家账号（手机号）
     * @param orderNumber 订单单号
     * @param orderType
     */
    public static void returnMoneyOrderPushByOrderStateToBusiness(Integer id, Integer userId, String phone, String orderNumber, Integer orderType) {
//        String title = "订单：" + orderNumber + " 的用户催单";
        String title = "您的订单有用户退单了，请及时处理";
//        String content = "您的订单:" + orderNumber + ",被用户催单了，请及时处理";
        String content = "您的订单有用户退单了，请及时处理";
        Map<String, Object> newMap = new HashMap<String, Object>();
        newMap.put("userId", userId);
        newMap.put("orderId", id);
        newMap.put("orderType", orderType);
        newMap.put("orderNumber", orderNumber);
        newMap.put("pushType", PushType.订单.getCode());
        newMap.put("sound", "tuikuan.mp3");
        JSONObject jsonObject = JSONObject.fromObject(newMap);
        UserJPushHelper jPushHelper = new UserJPushHelper("business.properties");
        jPushHelper.sendByAlias(title, content, jsonObject.toString(), phone, "tuikuan.mp3");
    }

    /**
     * 用户申请退款
     *
     * @param id          订单编号
     * @param userId      用户编号
     * @param phone       商家账号（手机号）
     * @param orderNumber 订单单号
     * @param orderType
     * @param orderState
     * @param reason
     */
    public static void returnMoneyOrderPushByOrderStateToDelivery(Integer id, Integer userId, String phone, String orderNumber, Integer orderType, Integer orderState, String reason) {
//        String title = "订单：" + orderNumber + " 的用户催单";
        String title = "很遗憾，您的订单已被买家取消，请重新抢单!";
//        String content = "您的订单:" + orderNumber + ",被用户催单了，请及时处理";
        String content = "很遗憾，您的订单已被买家取消，请重新抢单!";
        Map<String, Object> newMap = new HashMap<String, Object>();
        newMap.put("userId", userId);
        newMap.put("orderId", id);
        newMap.put("orderType", orderType);
        newMap.put("orderState", orderState);
        newMap.put("reason", reason);
        newMap.put("orderNumber", orderNumber);
        newMap.put("pushType", PushType.订单.getCode());
        newMap.put("sound", "tuikuan.mp3");
        JSONObject jsonObject = JSONObject.fromObject(newMap);
        UserJPushHelper jPushHelper = new UserJPushHelper("distribution.properties");
        jPushHelper.sendByAlias(title, content, jsonObject.toString(), phone, "tuikuan.mp3");
    }

    /**
     * 用户催单订单处于配送中催配送员
     *
     * @param id          订单编号
     * @param userId      用户编号
     * @param phone       配送员账号（手机号）
     * @param orderNumber 订单单号
     * @param orderType
     * @param reason
     */
    public static void reminderOrderPushByOrderStateToDelivery(Integer id, Integer userId, String phone, String orderNumber, Integer orderType, String reason) {
//        String title = "订单：" + orderNumber + " 的用户催单";
        String title = "您的订单被用户催单了，请及时查看";
//        String content = "您的订单:" + orderNumber + ",被用户催单了，请及时处理";
        String content = "您的订单被用户催单了，请及时查看";
        Map<String, Object> newMap = new HashMap<String, Object>();
        newMap.put("userId", userId);
        newMap.put("orderId", id);
        newMap.put("reason", reason);
        newMap.put("orderType", orderType);
        newMap.put("pushType", PushType.订单.getCode());
        newMap.put("orderNumber", orderNumber);
        newMap.put("sound", "cuidan.mp3");
        JSONObject jsonObject = JSONObject.fromObject(newMap);
        UserJPushHelper jPushHelper = new UserJPushHelper("distribution.properties");
        jPushHelper.sendByAlias(title, content, jsonObject.toString(), phone, "cuidan.mp3");
    }

    /**
     * @param id        订单编号
     * @param phone     配送员账号（手机号）
     * @param orderType
     */
    public static void newOrderPush(Integer id, List<String> phone, Integer orderType) {
//        String title = "订单：" + orderNumber + " 的用户催单";
        String title = "您有新的淘瞬达任务，请及时抢单！";
//        String content = "您的订单:" + orderNumber + ",被用户催单了，请及时处理";
        String content = "您有新的淘瞬达任务，请及时抢单！";
        Map<String, Object> newMap = new HashMap<String, Object>();
        newMap.put("orderId", id);
        newMap.put("orderType", orderType);
        newMap.put("pushType", PushType.订单.getCode());
        newMap.put("sound", "news.mp3");
        JSONObject jsonObject = JSONObject.fromObject(newMap);
        UserJPushHelper jPushHelper = new UserJPushHelper("distribution.properties");
        jPushHelper.sendByAlias(title, content, jsonObject.toString(), phone, "news.mp3");
    }

    /**
     * 用户催单订单处于配送中催配送员
     *
     * @param id          订单编号
     * @param userId      用户编号
     * @param busPhone    商家账号（手机号）
     * @param disPhone    配送员账号（手机号）
     * @param orderNumber 订单单号
     * @param orderType
     */
    public static void reminderOrderPushByOrderStateToAll(Integer id, Integer userId, String busPhone, String disPhone, String orderNumber, Integer orderType) {
//        String title = "订单：" + orderNumber + " 的用户催单";
        String title = "您的订单被用户催单了，请及时查看";
//        String content = "您的订单:" + orderNumber + ",被用户催单了，请及时处理";
        String content = "您的订单被用户催单了，请及时查看";
        Map<String, Object> newMap = new HashMap<String, Object>();
        newMap.put("userId", userId);
        newMap.put("orderId", id);
        newMap.put("orderType", orderType);
        newMap.put("pushType", PushType.订单.getCode());
        newMap.put("orderNumber", orderNumber);
        newMap.put("sound", "cuidan.mp3");
        JSONObject jsonObject = JSONObject.fromObject(newMap);
        UserJPushHelper companyJPushHelper = new UserJPushHelper("business.properties");
        companyJPushHelper.sendByAlias(title, content, jsonObject.toString(), busPhone, "");
        UserJPushHelper jPushHelper = new UserJPushHelper("distribution.properties");
        jPushHelper.sendByAlias(title, content, jsonObject.toString(), disPhone, "cuidan.mp3");
    }

    public static void applyReturnGoods(Integer id, Integer buyerId, String account, String orderNumber, Integer orderType) {
        //        String title = "订单：" + orderNumber + " 的用户催单";
        String title = "您有淘瞬达订单申请退换货，请及时查看!";
//        String content = "您的订单:" + orderNumber + ",被用户催单了，请及时处理";
        String content = "您有淘瞬达订单申请退换货，请及时查看!";
        Map<String, Object> newMap = new HashMap<String, Object>();
        newMap.put("userId", buyerId);
        newMap.put("orderId", id);
        newMap.put("orderType", orderType);
        newMap.put("orderNumber", orderNumber);
        newMap.put("pushType", PushType.订单.getCode());
        newMap.put("sound", "tuihuo.mp3");
        JSONObject jsonObject = JSONObject.fromObject(newMap);
        UserJPushHelper jPushHelper = new UserJPushHelper("business.properties");
        jPushHelper.sendByAlias(title, content, jsonObject.toString(), account, "tuihuo.mp3");
    }
}
